package com.oocl.crm.db;


public class MyLinkedList<T> {
	public class Node<T>{
		private Node<T> prev=null;
		private Node<T> next=null;
		private T data ;
		public Node(T t){
			this.data=t;
		}
		public Node(){
		}
		public Node<T> getPrev() {
			return prev;
		}
		public void setPrev(Node<T> prev) {
			this.prev = prev;
		}
		public Node<T> getNext() {
			return next;
		}
		public void setNext(Node<T> next) {
			this.next = next;
		}
		public T getData() {
			return data;
		}
		public void setData(T data) {
			this.data = data;
		}	
	}
	private Node<T> head = new Node<T>(null);
	private Node<T> pointer = head;
	private int size=0;
	
	public Node<T> getHead() {
		return head;
	}
	public int getSize()
	{
		return size;
	}
	public void addSize()
	{	
		size++;
	}
	public void reduceSize()
	{	
		size--;
	}
	public void addNode(T t) {
		Node<T> n = new Node<T>(t);
		if(head==pointer) {
			head.setNext(n);
			n.setPrev(head);
			pointer=n;
		}
		else{
			n.setPrev(pointer);
			pointer.setNext(n);
		}
		pointer=n;
		addSize();
	}
	public boolean deleteNode(T t) {
		Node<T> p;
		p=head;
		while(p.getNext()!=null)
		{
			if(p.getNext().getData()==t){
				p=p.getNext();
				if(p.getNext()==null)
				{
					p.getPrev().setNext(null);//p.prev.next=null;
					p=null;
					reduceSize();
					return true;
				}
				else
				{
					pointer=pointer.getPrev();
					p.getPrev().setNext(p.getNext());//p.prev.next=p.next;
					p.getNext().setPrev(p.getPrev());//p.next.prev=p.prev;
					p=null;
					reduceSize();
					return true;
				}
			}
			p=p.getNext();
		}
		return false;
	}
	public boolean updateNode(T target,T newdata){
		Node<T> p;
		p=head;
		while(p.getNext()!=null)
		{
			if(p.getNext().getData()==target){
				p.getNext().setData(newdata);
				return true;
			}
			p=p.getNext();
		}
		return false;
	}
}
